﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ProgressBar;
using VideoClips.WinUI.Models;
using VideoClips.WinUI.Common;

namespace VideoClips.WinUI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private int lineNum=0;
        private void btnStart_Click(object sender, EventArgs e)
        {            
            lstbResult.Items.Clear();
            logno("开始解析数据文件....");
            if (!System.IO.File.Exists("data.txt"))
            {
                log("找不到数据文件data.txt");
                return;
            }
            List<VideoCut> list = new List<VideoCut>();
            string[] ary;
            TimeSpan begin;
            TimeSpan end;
            int i = 0;
            foreach (string line in System.IO.File.ReadLines("data.txt"))
            {
                ary = line.Trim().Split(',');
                log("第" + ++i + "行：" + line.Trim());
                if (ary.Length != 3)
                {
                    log("数据：" + line.Trim() + ",格式不对");
                    continue;
                }
                if (!System.IO.File.Exists(ary[0]))
                {
                    log("文件：" + ary[0].Trim() + ",不存在");
                    continue;
                }
                if (!TimeSpan.TryParse(ary[1].Trim(), out begin))
                {
                    log("起点时间：" + ary[1].Trim() + ",格式不对");
                    continue;
                }
                if (!TimeSpan.TryParse(ary[2].Trim(), out end))
                {
                    log("截止时间：" + ary[2].Trim() + ",格式不对");
                    continue;
                }
                if (end <= begin)
                {
                    log("截止时间应该大于起点时间！！！！！");
                    continue;
                }
                list.Add(new VideoCut(ary[0], ary[1], (end - begin).ToString()));
            }
            logno("解析数据文件完毕，成功解析文件：" + list.Count + "个...");
            if (list.Count < 1)
            {
                log("没有数据，退出");
            }



            VideoCut c;
            string file;
            for (i = 0; i < list.Count; i++)
            {
                logno("开始剪切第【" + i + "】个文件...");
                c = list[i];
                file = FFMEPGHelper.Cut(c.file, c.begin, c.end);
                if (file.Length > 0)
                {
                    log("剪切成功，输出文件：" + file);
                }
                else log("剪切失败.....");
            }
            log("");
            log("全部剪切完成......");
        }

        private void logno(string v)
        {
            lineNum++;
            lstbResult.Items.Add(lineNum+","+v);
        }

        private void log(string v)
        {
            lstbResult.Items.Add("\t"+v);
        }
    }
}
